package com.sk.waste.app.views.webview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class AppChromeClient extends WebChromeClient
  {
    private final AppCompatActivity activity;
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;
    private ValueCallback<Uri[]> filePathCallback;
    private Uri cameraImageUri;

    public AppChromeClient(AppCompatActivity activity)
      {
        this.activity = activity;
        registerFilePickerLauncher();
      }

    private void registerFilePickerLauncher()
      {
        intentActivityResultLauncher = activity.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
              Uri[] results = null;
              if (result.getResultCode() == Activity.RESULT_OK)
                {
                  if (result.getData() != null && result.getData().getData() != null)
                    {
                      Uri dataUri = result.getData().getData();
                      results = new Uri[]{dataUri};
                    } else if (cameraImageUri != null)
                    {// 判断是否从相机获取返回结果
                      results = new Uri[]{cameraImageUri};
                    }
                }
              if (filePathCallback != null)
                {
                  filePathCallback.onReceiveValue(results);
                }
              filePathCallback = null;
              cameraImageUri = null;
            }
        );
      }

    @Override
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams)
      {
        this.filePathCallback = filePathCallback;

        // 创建相机意图
        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //      if (captureIntent.resolveActivity(activity.getPackageManager()) != null) {
        // 创建一个相机uri
        cameraImageUri = createImageUri();
        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri);
        //      }
        // 创建一个选择图片的意图
        //        Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT); //这个意图在某些机型上不会直接拉起图片选择器
        Intent pickIntent = new Intent(Intent.ACTION_PICK);
        pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        //        pickIntent.addCategory(Intent.CATEGORY_OPENABLE); //action_pick 不需要这个设置
        // 使用选择器让用户选择哪种方式
        Intent chooserIntent = Intent.createChooser(pickIntent, "选择从哪里获取图片");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{captureIntent});
        intentActivityResultLauncher.launch(chooserIntent);
        return true;
      }

    @SuppressLint("SimpleDateFormat")
    private Uri createImageUri()
      {
        ContentResolver resolver = activity.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DISPLAY_NAME, "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
        return resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
      }

  }
